package com.tuanbiao.presenter.auth;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.entity.PlanEntity;
import com.hzjq.data.hzjq.entity.CapRecordEntity;
import com.hzjq.lib.bean.PlanInfo;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.AuthController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.data.callback.OnDetonatorsCallback;
import com.tuanbiao.events.PlanEntityEvent;
import com.tuanbiao.utils.LibCollections;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/22 15:53
 */
public class AuthOnlinePresenter extends BasePresenter<AuthController.Model, AuthController.OnlineView> implements AuthController.OnlinePresenter {
    private PlanEntity mPlanEntity;

    public AuthOnlinePresenter(AuthController.Model model, AuthController.OnlineView rootView) {
        super(model, rootView);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void init(PlanEntityEvent event) {
        mPlanEntity = event.getEntity();
        EventBus.getDefault().removeStickyEvent(event);
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private void init() {
        mRootView.showProject(mModel.getCurrentProject());
        querySelectDetonator();
    }

    @Override
    public void downloadWorkCode(List<CapRecordEntity> entities) {
        if (LibCollections.isEmpty(entities)) {
            mRootView.showMessage("暂无雷管需要授权！");
            return;
        }
        mModel.auth(entities, new OnBaseResultCallback<List<CapRecordEntity>>() {
            @Override
            public void onSuccess(List<CapRecordEntity> entities) {
                Timber.i("downloadWorkCode:"+entities.toString());
                if (LibCollections.isEmpty(entities)) {
                    mModel.refreshAuth();
                    mRootView.showMessage("授权完成");
                } else {
                    mRootView.showMessage("还有" + LibCollections.size(entities) + "发未授权！");
                }
                querySelectDetonator();
            }

            @Override
            public void onFailed(String msg) {
                Timber.i("downloadWorkCode#onFailed:"+msg);
                mRootView.showMessage(msg);
            }
        });
    }

    private void querySelectDetonator() {
        Timber.i("querySelectDetonator:"+mPlanEntity);
        if (null != mPlanEntity) {
            mModel.queryDetonatorByPlanId(String.valueOf(mPlanEntity.getId()), new OnDetonatorsCallback() {
                @Override
                public void onSuccess(List<CapRecordEntity> entities, PlanInfo planInfo) {
                    mRootView.setDetonatorItems(entities);
                }

                @Override
                public void onFailed(String msg) {
                    mRootView.showMessage(msg);
                }
            });
        } else {
            mModel.querySelectDetonator(new OnDetonatorsCallback() {
                @Override
                public void onSuccess(List<CapRecordEntity> entities, PlanInfo planInfo) {
                    mRootView.setDetonatorItems(entities);
                }

                @Override
                public void onFailed(String msg) {
                    mRootView.showMessage(msg);
                }
            });
        }

    }
}
